<?php

namespace app\applet\controller;

use think\Validate;

Class StoreApply
{
    public function __construct()
    {
        $this->store = new \app\applet\model\StoreApply();
    }

    /**
     *店铺编辑
     */
    public function updateShop()
    {
        $input = input();
        $validate = new Validate([
            ['sa_id', 'require', '店铺ID不能为空'],
            ['ic_id', 'require', '行业分类ID不能为空'],
            ['phone', 'require', '联系人电话不能为空'],
            ['distribution_method', 'require', '配送方式不能为空'],
            ['delivery_type', 'require', '送货上门类型不能为空'],
//            ['advertisement', 'require', '联盟商家广告不能为空'],
            ['settle_ratio', 'require', '行业分类比例不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $bool = $this->store->updateShop($input);
        if($bool)
        {
            returnJson(200,"编辑完成");
        }else{
            returnJson(-2004,"编辑失败");
        }
    }
    public function listAdvertisement()
    {
        $data = $this->store->listAdvertisement();
        return returnJson(200,'success',$data);
    }
    public function ownAdvertisement()
    {
        $data = $this->store->ownAdvertisement();
        return returnJson(200,'success',$data);
    }
    public function addAdvertisement()
    {
        $input = input();
        $validate = new Validate([
            ['advertisement', 'require', '商家广告信息不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $this->store->addAdvertisement($input['advertisement']);
        return returnJson(200,'success');
    }
    public function ownAdvertisementAdd()
    {
        $input = input();
        $validate = new Validate([
            ['img', 'require', '图片不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $this->store->ownAdvertisementAdd($input['img']);
        return returnJson(200,'添加成功');

    }
    public function shopAdvert()
    {
        $input = input();
        $validate = new Validate([
            ['id', 'require', '店铺id不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $data = $this->store->shopAdvert($input['id']);
        return returnJson(200,'success',$data);
    }
    /**
     *联盟商家广告位添加,通过电话号码判断店铺是否存在
     * @param $phone string  电话
     */
    public function whetherShop()
    {
        $input = input();
        $validate = new Validate([
            ['phone', 'require', '商家注册手机号不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $data = $this->store->whetherShop($input['phone']);
        return returnJson(200,'商家存在',$data);

    }

    /**
     * 商铺行业分类下拉列表显示
     */
    public function industryClass()
    {
        $data = $this->store->industryClass();
        returnJson(200,'success',$data);
    }
    /**
     *查看店铺详情
     */
    public function shopDetail()
    {
        #根据token获取用户id
        $member_id = get_header_id("token");
        $data = $this->store->shopDetail($member_id);
        returnJson(200,'success',$data);
    }
    /**
     * 开店
     */
    public function openShop()
    {
        $input = input();
        $validate = new Validate([
            ['company_name', 'require', '企业或门店名称不能为空'],
            ['contact', 'require', '联系人姓名'],
            ['phone', 'require', '联系人电话不能为空'],
            ['address', 'require', '企业或门店地址不能为空'],
            ['longitude', 'require', '地址经度不能为空'],
            ['latitude', 'require', '地址纬度不能为空'],
            ['address_detail', 'require', '公司详细地址'],
            ['introduce', 'require', '商品或者服务介绍不能为空'],
            ['card', 'require', '身份证号码不能为空'],
            ['card_just', 'require', '身份证正面不能为空'],
            ['card_back', 'require', '身份证反面不能为空'],
            ['business_license', 'require', '营业执照不能为空'],
            ['credit_code', 'require', '信用代码不能为空'],
            ['mch_id', 'require', '微信商户号不能为空'],
            ['number', 'require', '已开实体店数量不能为空'],
            ['investment_phone', 'require', '招商专员手机号不能为空'],

        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $input['member_id'] = get_header_id('token');
        $input['status'] = 2; #申请中
        $input['create_time'] = time();

        #店铺唯一性判断
        $this->store->isOnly($input['member_id']);
        #商铺申请数据添加数据库
        $bool = $this->store->openShop($input);
        if($bool)
        {
            returnJson(200,'提交申请成功');
        }else{
            returnJson(-2004,'提交申请失败');
        }
    }

}